package com.example.springboot_ruiji.controller;

import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.pojo.Employee;
import com.example.springboot_ruiji.service.EmployeeService;
import com.example.springboot_ruiji.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Value("${file.path}")
    private String path;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        R<Employee> r=employeeService.login(employee);
        if (r.getCode()==1){
            session.setAttribute("employee",r.getData().getId());
        }
        return employeeService.login(employee);
    }
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,@RequestParam(required = false) String name){
        pageSize=10;
        return employeeService.page(page,pageSize,name);
    }
    @PostMapping
    public R add(@RequestBody Employee employee){
        return employeeService.add(employee);
    }

    @GetMapping("/{id}")
    public R toEdit(@PathVariable Long id){
        return employeeService.toEdit(id);
    }

    @PutMapping
    public R updateStatus(@RequestBody Employee employee,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        return employeeService.edit(employee);
    }

    //文件上传
    @PostMapping("upload")
    public R<String> upload(MultipartFile file){
        //定义一个变量
        String originalFilename = null;
        try {
            //获取文件名
            originalFilename=file.getOriginalFilename();
            //分割
            String name= UUID.randomUUID().toString().replaceAll("-","");

            //重命名以后的文件夹名字
            String fileName=name+ FileUtils.getSuffix(originalFilename);
            //获取文件
            File dir=new File(path);
            //判断文件是否存在
            if (!dir.exists()){
                dir.mkdirs();
            }
            File file1 = new File(path, fileName);
            //获取文件路径
            file.transferTo(file1);
            log.info(fileName);
            return R.success(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("服务器内部错误");
    }

    //下载
    @GetMapping("download")
    public byte[] download(String name) throws IOException {
        log.error("name:{}",name);
        File file = new File(path, name);
        if (file.exists()){
            return FileUtils.File2byte(file);
        }
        return null;
    }

}
